/**
 * Definition for singly-linked list. */
  struct ListNode {
      int val;
      ListNode *next;
      ListNode() : val(0), next(nullptr) {}
      ListNode(int x) : val(x), next(nullptr) {}
      ListNode(int x, ListNode *next) : val(x), next(next) {}
  };
#include<vector>
class Solution {
public:
    ListNode* partition(ListNode* head, int x) {
        std::vector<ListNode*> smaller;
        auto bigger = smaller;
        ListNode* cur = head;
        while (cur) {
            if (cur->val < x)
                smaller.emplace_back(cur);
            else
                bigger.emplace_back(cur);
            cur = cur->next;
        }
        ListNode* newhead = new ListNode(-1);
        cur = newhead;
        for (auto& e : smaller) {
            cur->next = e;
            cur = cur->next;
        }
        cur->next = nullptr;
        for (auto& e : bigger) {

            cur->next = e;
            cur = cur->next;
        }
        cur->next = nullptr;

        return newhead->next;
    }
};